/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlo;

import dao.AreaCientificaDao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import modelo.AreaCientifica;
import modelo.Curso;
import modelo.InstituicaoAcademica;

/**
 *
 * @author bruno
 */
@ManagedBean
@SessionScoped
public class AreaCientificaControlo implements Serializable {

    /**
     * Creates a new instance of AreaCientifica
     */
    AreaCientifica areaCientificaSelecionada;
    List<AreaCientifica> areaCientificaLista;
    List<AreaCientifica> areaCientificaDependenteLista;
    @ManagedProperty(value = "#{instituicaoAcademicaControlo}")
    InstituicaoAcademicaControlo instituicaoAcademicaControlo;

    public AreaCientificaControlo() {
        areaCientificaSelecionada = new AreaCientifica();
        areaCientificaLista = new AreaCientificaDao().lerTodos();
        areaCientificaDependenteLista = new ArrayList<AreaCientifica>();
    }

    public AreaCientifica getAreaCientificaSelecionada() {
        return areaCientificaSelecionada;
    }

    public void setAreaCientificaSelecionada(AreaCientifica areaCientificaSelecionada) {
        this.areaCientificaSelecionada = areaCientificaSelecionada;
    }

    public void setInstituicaoAcademicaControlo(InstituicaoAcademicaControlo instituicaoAcademicaControlo) {
        this.instituicaoAcademicaControlo = instituicaoAcademicaControlo;
    }

    public List<AreaCientifica> autoAreas(String inst) {
        List<AreaCientifica> modeloLista = new ArrayList<AreaCientifica>();

        for (AreaCientifica modelo : areaCientificaDependenteLista) {
            if (modelo.getDesignacao().toLowerCase().startsWith(inst.toLowerCase())) {
                modeloLista.add(modelo);
            }
        }

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(modeloLista.toString()));
        return modeloLista;
    }

    public void actualizarAreaComInstituicao(InstituicaoAcademica ia) {
        areaCientificaDependenteLista.clear();
        for (InstituicaoAcademica iaTemp : instituicaoAcademicaControlo.getInstituicoesLista()) {
            if (ia.getId() == iaTemp.getId()) {
                ia = iaTemp;
                break;
            }
        }
        for (AreaCientifica ac : areaCientificaLista) {
            for (Curso c : ia.getCursoLista()) {
                if (ac.getId() == c.getAreaCientifica().getId()) {
                    areaCientificaDependenteLista.add(ac);
                    break;
                }
            }
        }


        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Nova Lista Areas-" + ia.getId() + "  \n:" + areaCientificaDependenteLista.toString()));
    }
}
